﻿using Autodesk.AutoCAD.DatabaseServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace AutoCadConvert
{
    public class Door : Cube
    {
        public bool Mirror=false;//镜像旋转

        public Door()
        {
            Height = 2100;
        }
        public override IEnumerable<XElement> ToXml()
        {
            return base.ToXml().Concat(new XElement[] {
                new XElement("Mirror",Mirror?1:0)
            });
        }
    }

    public class Doors: Collection
    {
        public List<Door> m_Door = new List<Door>();
        public Doors(House house) : base(house){}
        public override void AjustLineAndPoint()
        {
            //位置矫正
            Point center = m_house.Item<Points>().m_Center;
            foreach (BspObject obj in m_house.objList)
            {
                Collection collect = obj as Collection;
                if (collect != null)
                {
                    foreach (BspObject item in collect.m_EntityList)
                    {
                        if (item is Block)
                        {
                            Block bref = item as Block;
                            if (bref.m_Block.GetValueAttributesInBlock("图例分类") == "硬装" 
                                && bref.m_Block.GetValueAttributesInBlock("图例编码") == "door")
                            {
                                List<Point> ptls = new List<Point>();
                                Point basePt = new Point(bref.m_Block.Position);
                                Block.RecurBlockReference(bref.m_Block, bref.m_Block.ScaleFactors, new Point(bref.m_Block.Position), center, ref ptls);//解析数据
                                if (ptls.Count == 0) continue;
                                double dMaxX = ptls.Max(o => o.X), dMaxY = ptls.Max(o => o.Y), dMinX = ptls.Min(o => o.X), dMinY = ptls.Min(o => o.Y);

                                //属性字段
                                bool bMirror = false;
                                double dHeight;
                                double.TryParse(bref.m_Block.GetValueAttributesInBlock("高度"), out dHeight);
                                Boolean.TryParse(bref.m_Block.GetValueAttributesInBlock("镜像"), out bMirror);
                                m_Door.Add(new Door()
                                {
                                    Width = dMaxY - dMinY,
                                    Thickness = 120,
                                    Pos = (basePt - center),
                                    Rotation = bref.m_Block.Rotation / Math.PI * 180 +90,
                                    Height = dHeight==0?2100:dHeight,
                                    Mirror = bMirror,
                                });
                            }
                        }
                    }
                }
            }
        }
        public override IEnumerable<XElement> ToXml()
        {
            int idx = 0;
            foreach (Door dr in m_Door)
                yield return new XElement("_" + idx++, dr.ToXml());
        }
    }
}
